package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.pojo.MemberOrder;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetmealDao setmealDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //保存会员信息
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<MemberOrder> page = memberDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增预约信息
     * @param memberOrder
     * @param setmealIds
     */
    @Override
    public void insert(MemberOrder memberOrder, Integer[] setmealIds) {
        //新增成员信息，操作t_member表
        Member member = new Member();
        member.setId(memberOrder.getId());
        member.setName(memberOrder.getName());
        member.setSex(memberOrder.getSex());
        member.setPhoneNumber(memberOrder.getPhoneNumber());
        member.setAge(memberOrder.getAge());
        member.setIdCard(memberOrder.getIdCard());
        member.setBirthday(memberOrder.getBirthday());
        member.setStatus(memberOrder.getStatus());
        memberDao.add(member);
        //获取成员ID
        Integer id = member.getId();
        //新增预约信息
        Order order = new Order();
        order.setMemberId(id);
        order.setOrderDate(memberOrder.getOrderDate());
        order.setOrderStatus(memberOrder.getOrderStatus());
        order.setOrderType(memberOrder.getOrderType());
        if(setmealIds != null && setmealIds.length > 0){
            for (Integer setmealId : setmealIds) {
                order.setSetmealId(setmealId);
            }
        }
        orderDao.add(order);

    }

    /**
     * 根据id删除数据
     * @param id
     */
    @Override
    public void orderMake(Integer id) {
        //根据id删除order表
        orderDao.deleteById(id);
        memberDao.deleteById(id);
    }

    //根据id批量删除数据
    @Override
    public void delOrderById(Integer[] delIds) {
        //遍历数组
        for (Integer id : delIds) {
            //调用dao层的方法，删除order表中的数据
            orderDao.deleteById(id);
            memberDao.deleteById(id);
        }
    }

}
